package com.lenovo.gpsautotest;

import android.content.ContentResolver;
import android.content.Context;
import android.location.Criteria;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationManager;
import android.provider.Settings;


/**
 * @ProjectName: GpsAutoTest
 * @Package: com.lenovo.gpsautotest
 * @ClassName: LocationAssistant
 * @Description: LocationAssistant类作用描述
 * @Author: kgzhou
 * @CreateDate: 2019/5/29 17:44
 * @UpdateUser: kgzhou
 * @UpdateDate: 2019/5/29 17:44
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class LocationAssistant {

    private static LocationManager locationManager = null;

    public static LocationManager getManagerInstance(Context context) {
        if (locationManager == null) {
            synchronized (LocationAssistant.class) {
                if (locationManager == null) {
                    locationManager =
                            (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
                }
            }
        }
        return locationManager;
    }

    public static LocationResult locateByGPS(Context context) {
        Location location =
                getManagerInstance(context).getLastKnownLocation(getManagerInstance(context).getBestProvider(getCriteria(),true));
        LocationResult result = new LocationResult();
        result.setLongitude(location.getLongitude());
        result.setLatitude(location.getLatitude());
        double inputLongitude=Double.parseDouble(SpUtils.getInputLongitude(context));
        double inputLatitude=Double.parseDouble(SpUtils.getInputLatitude(context));
        result.setDistance(Utils.getDistance(location.getLongitude(),location.getLatitude(),
                inputLongitude,inputLatitude));
        //海拔
        if (location.hasAltitude()) {
            result.setAltitude(location.getAltitude());
        }
        //方向
        if (location.hasBearing()) {
            result.setBearing(location.getBearing());
        }
        //速度
        if (location.hasSpeed()) {
            result.setSpeed(location.getSpeed());
        }
        //时间
        result.setTime(Utils.formatUTC(location.getTime(), "yyyy-MM-dd HH:mm:ss"));
        //获取当前卫星状态
        GpsStatus gpsStatus=getManagerInstance(context).getGpsStatus(null);
        //获取卫星颗数的默认最大值
        result.setMaxSatellites(gpsStatus.getMaxSatellites());
        //获取Time To First Fix值
        result.setTimeToFirstFix(gpsStatus.getTimeToFirstFix());
        //迭代器保存所有卫星
        result.setGpsSatelliteIterator(gpsStatus.getSatellites().iterator());
        return result;
    }

    private static Criteria getCriteria() {
        Criteria criteria = new Criteria();
        criteria.setAccuracy(1);
        criteria.setSpeedRequired(false);
        criteria.setCostAllowed(false);
        criteria.setBearingRequired(false);
        criteria.setAltitudeRequired(false);
        criteria.setPowerRequirement(1);
        return criteria;
    }


    /**
     * 判断GPS是否开启
     *
     * @return
     */
    public static boolean isGPSAvailable(Context context) {
        // 获取所有可用的位置提供器
        return getManagerInstance(context).isProviderEnabled(LocationManager.GPS_PROVIDER);
    }


    //冷启动之前删除先前已下载的A-GPS 数据
    public static void deleteAidingData(Context context) {
        LocationAssistant.getManagerInstance(context).sendExtraCommand(LocationManager.GPS_PROVIDER,
                "delete_aiding_data", null);
    }


    public static void getSateInfo(){

    }

}
